"""
演示Python的装饰器特性
"""


# 装饰器的一般写法（闭包）
# def outer(func):
#     def inner():
#         print("我要睡觉了")
#         result = func()
#         print("我起床了")
#         return result
#
#     return inner
#
#
# def sleep():
#     import random
#     import time
#     print("睡眠中...")
#     duration_time = random.randint(1, 6)
#     time.sleep(duration_time)
#     return duration_time
#
#
# fn = outer(sleep)
# duration_time = fn()
# print(f"睡眠时间：{duration_time}，数据类型：{type(duration_time)}")

# 装饰器的快捷写法（语法糖）
def outer(func):
    def inner():
        print("我要睡觉了")
        result = func()
        print("我起床了")
        return result

    return inner


# 语法糖写法，给sleep函数增加了一个outer装饰器，相当于将sleep函数传入outer函数
@outer
def sleep():
    import random
    import time
    print("睡眠中...")
    duration_time = random.randint(1, 6)
    time.sleep(duration_time)
    return duration_time


duration_time = sleep()
print(f"睡眠时间：{duration_time}，数据类型：{type(duration_time)}")
